﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using DomainModel.Entities;
using System.Data.Linq;
using Rhino.Commons;
using NHibernate.Criterion;

namespace DomainModel.Concrete
{
    public class ProductsRepository : IProductsRepository
    {

        #region IProductsRepository Members

        public void Add(Product product)
        {
            UnitOfWork.CurrentSession.Save(product);
        }
        #endregion

        #region IProductsRepository Members


        public void Update(Product product)
        {
            throw new NotImplementedException();
        }

        public void Remove(Product product)
        {
            throw new NotImplementedException();
        }

        public Product GetById(int productId)
        {
            return (Product)UnitOfWork.CurrentSession.
                CreateQuery("select prod from Product prod where prod.ProductID =:productId")
                .SetInt64("productId", productId).UniqueResult();

            //var criteria = DetachedCriteria.For(typeof(Product));
            //criteria.Add(new SimpleExpression("productId", productId, "=", true));
            //return UnitOfWork.CurrentSession.ge
        }

        public Product GetByName(string name)
        {
            throw new NotImplementedException();
        }

        public ICollection<Product> GetByCategory(string category)
        {
            ICollection<Product> results =
                UnitOfWork.CurrentSession.
                CreateQuery("select prod from Product prod where prod.Category =:category ")
                .SetString("category", category)
                .List<Product>();

            return results;
        }

        #endregion
    }
}
